package com.qf.service.Impl;

import com.qf.bean.CartBean;
import com.qf.dao.AddressesDao;
import com.qf.dao.Impl.AddressesDaoImpl;
import com.qf.dao.Impl.OrdersDaoImpl;
import com.qf.dao.Impl.UserDaoImpl;
import com.qf.dao.OrdersDao;
import com.qf.dao.UserDao;
import com.qf.pojo.Address;
import com.qf.pojo.Orders;
import com.qf.pojo.User;
import com.qf.service.OrdersService;

import java.util.List;

public class OrdersServiceImpl implements OrdersService {

    private OrdersDao ordersDao = new OrdersDaoImpl();
    private AddressesDao addressesDao = new AddressesDaoImpl();
    private UserDao userDao = new UserDaoImpl();

    @Override
    public void add(Integer aid,Integer uid, List<CartBean> cartBeanList) {
        //通过uid查询用户的地址信息
        Address address = addressesDao.findById(aid);
        //通过uid查询用户信息
        User db_user = userDao.findById(uid);

        //设置购物车的总价
        double sumPrice = 0.0;

        //获取购物车信息
        for (CartBean cartBean : cartBeanList){
            //第一个商品的总价
            Double realPrice = cartBean.getRealPrice();
            Integer num = cartBean.getNum();
            //设置某件商品的总价
            Double singleProductPrice = realPrice * num;
            //加入到总价格
            sumPrice += singleProductPrice;
        }

        System.out.println("商品的总价：" + sumPrice);

        //封装订单对象，插入到数据库
        Orders orders = new Orders();
        orders.setUid(uid);
        orders.setRecv_name(db_user.getUsername());
        orders.setRecv_phone(address.getPhone());
        orders.setRecv_province(address.getProvince_name());
        orders.setRecv_city(address.getCity_name());
        orders.setRecv_area(address.getArea_name());
        orders.setRecv_address(address.getAddress());
        orders.setTotal_price(sumPrice);

        System.out.println(orders);

    }
}
